﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using ProjectWebNhac.DTO;
using System.Data;

namespace ProjectWebNhac.GUI.Admin
{
    public partial class QL_NguoiDung : System.Web.UI.Page
    {
        NguoiDungBus ndbus = new NguoiDungBus();
        NguoiDungDTO nddto = new NguoiDungDTO();
        BaiHatBus bhbus = new BaiHatBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                loadtrang();
            }
        }
        private void loadtrang()
        {
            gvDSNguoiDung.DataSource = ndbus.LayDanhSachNguoiDung();
            gvDSNguoiDung.DataBind();
        }

        protected void gvDSBaiHatNguoiDung_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Xóa"))
            {
                nddto.TaiKhoan = txtTaiKhoan.Text;
                int MaBaiHat = int.Parse(e.CommandArgument.ToString());
                bhbus.XoaBai(MaBaiHat);
                loadtrang();
                
                DataTable dt = ndbus.LayThongTinNguoiDung(nddto);
                txtTaiKhoan.Text = dt.Rows[0]["TaiKhoan"].ToString();
                txtNgaySinh.Text = dt.Rows[0]["NgaySinh"].ToString();
                txtEmail.Text = dt.Rows[0]["Email"].ToString();
                txtNgayDangKy.Text = dt.Rows[0]["NgayDangKy"].ToString();
                txtTenNguoiDung.Text = dt.Rows[0]["HoTen"].ToString();
                imgHinhAnh.ImageUrl = "../../HinhAnh/ThanhVien/" + dt.Rows[0]["Avatar"].ToString();
                string GioiTinh = dt.Rows[0]["HoTen"].ToString();
                if (GioiTinh == "True")
                    txtGioiTinh.Text = "Nam";
                else
                    txtGioiTinh.Text = "Nữ";
                string TrangThai = dt.Rows[0]["TrangThai"].ToString();
                if (TrangThai == "True")
                    lnkXoa.Text = "Khóa";
                else
                    lnkXoa.Text = "Mở";
                DataTable dt2 = bhbus.LoadDanhSachBaiHatTheoUser(nddto.TaiKhoan);
                gvDSBaiHatNguoiDung.DataSource = dt2;
                gvDSBaiHatNguoiDung.DataBind();
            }
        }

        protected void gvDSNguoiDung_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("ChiTiet"))
            {
                nddto.TaiKhoan  = e.CommandArgument.ToString();
                DataTable dt = ndbus.LayThongTinNguoiDung(nddto);
                txtTaiKhoan.Text = dt.Rows[0]["TaiKhoan"].ToString();
                txtNgaySinh.Text = dt.Rows[0]["NgaySinh"].ToString();
                txtEmail.Text = dt.Rows[0]["Email"].ToString();
                txtNgayDangKy.Text = dt.Rows[0]["NgayDangKy"].ToString();
                txtTenNguoiDung.Text = dt.Rows[0]["HoTen"].ToString();
                imgHinhAnh.ImageUrl = "../../HinhAnh/ThanhVien/" + dt.Rows[0]["Avatar"].ToString();
                string GioiTinh = dt.Rows[0]["HoTen"].ToString();
                if (GioiTinh == "True")
                    txtGioiTinh.Text = "Nam";
                else
                    txtGioiTinh.Text = "Nữ";
                string TrangThai = dt.Rows[0]["TrangThai"].ToString();
                if (TrangThai == "True")
                    lnkXoa.Text = "Khóa";
                else
                    lnkXoa.Text = "Mở";
                DataTable dt2 = bhbus.LoadDanhSachBaiHatTheoUser(nddto.TaiKhoan);
                gvDSBaiHatNguoiDung.DataSource = dt2;
                gvDSBaiHatNguoiDung.DataBind();
            }
            if (e.CommandName.Equals("Khoa"))
            {
                nddto.TaiKhoan = e.CommandArgument.ToString();
                ndbus.KhoaMoUser(nddto.TaiKhoan, 0);
                loadtrang();
            }
            if (e.CommandName.Equals("Mo"))
            {
                nddto.TaiKhoan = e.CommandArgument.ToString();
                ndbus.KhoaMoUser(nddto.TaiKhoan, 1);
                loadtrang();
            }
        }

        protected void lnkXoa_Click(object sender, EventArgs e)
        {
            if(lnkXoa.Text=="Khóa")
            {
                nddto.TaiKhoan= txtTaiKhoan.Text;
                ndbus.KhoaMoUser(nddto.TaiKhoan, 0);
                loadtrang();
            }
            else
            {
                string TaiKhoan = txtTaiKhoan.Text;
                ndbus.KhoaMoUser(TaiKhoan, 1);
                loadtrang();
            }   
        }

        protected void gvDSNguoiDung_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Label lbTrangThai = (Label)e.Row.FindControl("lbTrangThai");
                LinkButton lnkKhoa = (LinkButton)e.Row.FindControl("lnkKhoa");
                LinkButton lnkMo = (LinkButton)e.Row.FindControl("lnkMo");
                if(lbTrangThai.Text=="True")
                {
                    lbTrangThai.Text = "Mở";
                    lnkKhoa.Visible = true;
                    lnkMo.Visible = false;
                }
                else
                {
                    lbTrangThai.Text = "Khóa";
                    lnkKhoa.Visible = false;
                    lnkMo.Visible = true;
                }
            }
        }

    }
}